package core.utils;

import java.util.Calendar;

/**
 * @author sergiu.marin
 */
public class AuthUtils {

    /**
     * [year][month][day][hour]
     *
     * @param pwd an int number used for authentication. The value has following: [year][month][day][hour]
     * @return true if the value passes the validation, false otherwise
     */
    public static boolean isAuthValid(int pwd) {
        String value = String.valueOf(pwd);
        if (value.length() != 8) {
            return false;
        }

        Calendar c = Calendar.getInstance();
        boolean result;
        try {
            result = (String.valueOf(c.get(Calendar.YEAR)).endsWith(value.substring(0, 2)));
            result &= ((c.get(Calendar.MONTH) + 1) == Integer.valueOf(value.substring(2, 4)));
            result &= (c.get(Calendar.DAY_OF_MONTH) == Integer.valueOf(value.substring(4, 6)));
            result &= (c.get(Calendar.HOUR_OF_DAY) == Integer.valueOf(value.substring(6, 8)));
        } catch (NumberFormatException e) {
            return false;
        }

        return result;
    }

}
